package com.classic.mafiacards;



import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews.RemoteView;

/**
 * This widget display an analogic clock with two hands for hours and
 * minutes.
 */
@RemoteView
public class AnalogTimer extends View {


    private Drawable SecondsHand;

    private Drawable mDial;

    private int mDialWidth;
    private int mDialHeight;

    private float mSeconds;

    Context mContext;

    public AnalogTimer(Context context) {
        super(context);
    }

    public AnalogTimer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnalogTimer(Context context, AttributeSet attrs,
                       int defStyle) {
        super(context, attrs, defStyle);
        Resources r = context.getResources();
        TypedArray a =
                context.obtainStyledAttributes(
                        attrs, R.styleable.AnalogClock, defStyle, 0);
        mContext=context;

        mDial = r.getDrawable(R.drawable.clock_dial);

        SecondsHand = r.getDrawable(R.drawable.clock_hour);

        mDialWidth = mDial.getIntrinsicWidth();
        mDialHeight = mDial.getIntrinsicHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize =  MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize =  MeasureSpec.getSize(heightMeasureSpec);

        float hScale = 1.0f;
        float vScale = 1.0f;

        if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
            hScale = (float) widthSize / (float) mDialWidth;
        }

        if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
            vScale = (float)heightSize / (float) mDialHeight;
        }

        float scale = Math.min(hScale, vScale);

        setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
                resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int availableWidth = mDial.getIntrinsicWidth();
        int availableHeight = mDial.getIntrinsicHeight();

        int x = availableWidth / 2;
        int y = availableHeight / 2;

        final Drawable dial = mDial;
        int w = dial.getIntrinsicWidth();
        int h = dial.getIntrinsicHeight();

        boolean scaled = false;

        if (availableWidth < w || availableHeight < h) {
            scaled = true;
            float scale = Math.min((float) availableWidth / (float) w,
                    (float) availableHeight / (float) h);
            canvas.save();
            canvas.scale(scale, scale, x, y);
        }

        dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        dial.draw(canvas);

        canvas.save();

        canvas.restore();

        canvas.save();
        canvas.rotate(mSeconds / 60 * 360, x, y);
        w = SecondsHand.getIntrinsicWidth();
        h = SecondsHand.getIntrinsicHeight();
        SecondsHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        SecondsHand.draw(canvas);
        canvas.restore();

        canvas.save();

        canvas.restore();
        if (scaled) {
            canvas.restore();
        }
    }

    public void setTime(float seconds)
    {

        mSeconds = seconds;

    }
}
